<?php

/* * 
 * 挑战
 */
namespace Api\Model;
use Think\Model;
class ChallengeModel extends Model {

    //映射
    protected $_map = array(

    );

    //获取挑战
    public function getChallengeById($id) {

        $challenge = $this->where(array('challenge_id' => $id))->find();

        return $challenge;

    }
    
    //挑战主题列表（3条记录）
    public function getChallengeListBasicByType($challengeType) {

        $map['status'] = C("CHALLENGE_STATUS.START");
        $map['challenge_type'] = $challengeType;

        $list = $this
            ->field("challenge_id, challenge_name, challenge_image_url, join_count, category_name")
            ->where($map)
            ->order("join_count DESC")
            ->limit(3)
            ->select();

        return $list;

    }

    //挑战主题列表
    public function getChallengeList($challengeType, $categoryId='', $order=1, $pageNum, $userId) {

        $map['status'] = C("CHALLENGE_STATUS.START");
        $map['challenge_type'] = $challengeType;

        if(!isNullOrEmpty($categoryId)) {
            $map['category_id'] = $categoryId;
        }

        if($order == C('CHALLENGE_ORDER.HOTTEST')) {
            $order = "join_count DESC";
        } else {
            $order = "create_time DESC";
        }

        $list = $this
            ->field("challenge_id, challenge_name, challenge_image_url, join_count")
            ->where($map)
            ->order($order)
            ->limit(($pageNum-1)*C('DEFAULT_PAGE_SIZE') . ',' . C('DEFAULT_PAGE_SIZE'))
            ->select();

        foreach ($list as &$item) {

            $sql = "SELECT DATE_FORMAT(cj.create_time,'%m-%d') AS create_time,
                    (SELECT COUNT(1) + 1 FROM ff_challenge_join chj
                    WHERE chj.challenge_id = cj.challenge_id
                    AND chj.collect_count > cj.collect_count) AS rank  /*分数相同,名次并列*/
                    FROM ff_challenge_join cj
                    WHERE cj.user_id = " .$userId. " AND cj.challenge_id = " .$item['challenge_id'];

            $join = M()->query($sql);

            if(!isNullOrEmpty($join)) {
                $item['is_join'] = true;
                $item['join_time'] = $join[0]['create_time'];
                $item['rank'] = $join[0]['rank'];
            } else {
                $item['is_join'] = false;
            }

        }

        return $list;

    }

    //好友的挑战（3条记录）
    public function getFriendChallengeListBasic($challengeType, $userId) {

        $map['f.user_id'] = $userId;
        $map['c.challenge_type'] = $challengeType;

        $list = $this->alias('c')
            ->join("INNER JOIN __CHALLENGE_JOIN__ cj ON c.challenge_id = cj.challenge_id")
            ->join("INNER JOIN __FRIEND__ f ON cj.user_id = f.friend_user_id")
            ->field("c.challenge_id, c.challenge_name, c.challenge_image_url, c.category_name, 
                    COUNT(f.friend_user_id) as friend_count")
            ->where($map)
            ->group("c.challenge_id")
            ->order("c.join_count DESC")
            ->limit(3)
            ->select();

        return $list;

    }

    //好友的挑战
    public function getFriendChallengeList($challengeType, $userId, $pageNum) {

        $map['c.challenge_type'] = $challengeType;
        $map['f.user_id'] = $userId;

        $list = $this->alias('c')
            ->join("INNER JOIN __CHALLENGE_JOIN__ cj ON c.challenge_id = cj.challenge_id")
            ->join("INNER JOIN __FRIEND__ f ON cj.user_id = f.friend_user_id")
            ->field("c.challenge_id, c.challenge_name, c.challenge_image_url, c.join_count, c.category_name, 
                    COUNT(f.friend_user_id) as friend_count")
            ->where($map)
            ->group("c.challenge_id")
            ->order("c.join_count DESC")
            ->limit(($pageNum-1)*C('DEFAULT_PAGE_SIZE') . ',' . C('DEFAULT_PAGE_SIZE'))
            ->select();

        if(!isNullOrEmpty($list)) {
            foreach ($list as &$item) {
                $friendMap['f.user_id'] = $userId;
                $friendMap['cj.challenge_id'] = $item['challenge_id'];

                $item['friend_list'] = D('challengeJoin')->alias('cj')
                    ->join("INNER JOIN __FRIEND__ f ON cj.user_id = f.friend_user_id")
                    ->join("LEFT JOIN __USER__ u on f.friend_user_id = u.user_id")
                    ->field("u.user_id, u.head_image_url")
                    ->where($friendMap)
                    ->order("cj.create_time ASC")
                    ->limit(5)
                    ->select();
            }
        }

        return $list;
        
    }

}

